/*
 * Copyright (c) 2009 DeledImporter
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'Pyro Games' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.pyrogames.jmetest;

import net.pyrogames.deled.importer.DXSImporter;
import net.pyrogames.jme.ExtendedJME2Format;
import net.pyrogames.jme.extension.LightNodeExtension;

import com.jme.app.SimpleGame;
import com.jme.light.LightNode;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jme.scene.state.CullState;

/**
 * Simple test in jMonkey 2.0 that shows a scene
 * imported from a dxs file with lights attached. Also
 * this uses an extension to put these lights into light nodes.
 * 
 * @author Nicholas Woodfield
 *
 */
public class HelloDXSLights extends SimpleGame {

	public static void main(String[] args){
		HelloDXSLights app = new HelloDXSLights();
		app.start();
	}
	
	@Override
	protected void simpleInitGame() {
		//Turn off global light
		lightState.setEnabled(false);
		
		//Create the importer
		DXSImporter dxs = new DXSImporter();
		
		//Use a light node extension (rather than just attaching
		//lights to the model's root lightstate, we attach them to nodes,
		//so we don't have to worry about them when we move the model around!
		dxs.addExtension(new LightNodeExtension());
		
		//Create your format
		ExtendedJME2Format format = new ExtendedJME2Format();
		
		//Set importer attributes
		format.setScale(.25f);
		format.setTextureDirectory(HelloDXSImporter.class.getClassLoader().getResource("net/pyrogames/jmetest/data/"));
		
		//Import the data
		dxs.importModel(HelloDXSImporter.class.getClassLoader().getResource("net/pyrogames/jmetest/data/LightTest.dxs"), format);
		
		//Attach to the scene (or use format.save(...) to save to a file)
		rootNode.attachChild(format.getData());
		
		//Set a cull state
		CullState cs = display.getRenderer().createCullState();
		cs.setEnabled(true);
		cs.setCullFace(CullState.Face.Back);
		rootNode.setRenderState(cs);
		rootNode.updateRenderState();
		
		//Update world vectors, so we can place boxes at where the light locations
		//are in the scene
		rootNode.updateWorldVectors(true);
		
		Node lightVis = new Node("where the lights are in scene");
		//Visualize the light nodes
		for(Spatial s : format.getData().getChildren()){
			if(s instanceof LightNode){
				Box b = new Box("Light", new Vector3f(0,0,0), .5f, .5f, .5f);
				b.getLocalTranslation().set(((LightNode)s).getWorldTranslation());
				lightVis.attachChild(b);
			}
		}
		format.getData().attachChild(lightVis);
		
		//Recommended to flush the importer and format if you're reusing them
		dxs.flush();
		format.flush();
		//And if you re-use the importer and want to reset extensions
		dxs.flushExtensions();
	}
}
